package rpg.data;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import rpg.map.Location;
import square.data.ItemData;
import square.data.MapData;

/**
 * 
 * @author Pedro Silva Moreira
 */
public class XML {

	public static final String DATA_FOLDER = "data" + File.separator;

	// Load
	@SuppressWarnings("unchecked")
	public static void load(LoaderXML loader, String fileName,
			String elementName) {
		System.out
				.print("Loading " + loader.getClass().getSimpleName() + "...");

		Element elem = XML.loadXML(DATA_FOLDER + fileName + ".xml");
		List<Element> groups = elem.getChildren("group");

		for (int i = 0; i < groups.size(); i++) {
			String group = groups.get(i).getAttributeValue("name");

			List<Element> elements = groups.get(i).getChildren(elementName);
			for (int j = 0; j < elements.size(); j++) {
				String name = elements.get(j).getAttributeValue("name");
				try {
					loader.load(group, name, elements.get(j));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(" Finish");
	}

	// Save
	public static void saveMap(MapData map) {
		Element root = new Element("map");

		// Attributes
		root.setAttribute("name", map.getName());
		root.setAttribute("x", String.valueOf(map.getWidth()));
		root.setAttribute("y", String.valueOf(map.getHeight()));
		root.setAttribute("z", String.valueOf(map.getThick()));

		// Children
		for (int x = 0; x < map.getWidth(); ++x) {
			for (int y = 0; y < map.getHeight(); ++y) {
				for (int z = 0; z < map.getThick(); ++z) {
					Location location = (Location) map.getLocation(x, y, z);

					Element squareElement = new Element("square");
					for (ItemData it : location.getItems()) {
						Element itemElement = new Element("item");
						itemElement.setAttribute("name", it.getName());
						squareElement.addContent(itemElement);
					}

					if (location.getCreature() != null) {
						Element creatureElement = new Element("creature");
						creatureElement.setAttribute("name", location
								.getCreature().getName());
						squareElement.addContent(creatureElement);
					}

					if (location.getItems().size() > 0) {
						squareElement.setAttribute("x",
								String.valueOf(location.getX()));
						squareElement.setAttribute("y",
								String.valueOf(location.getY()));
						squareElement.setAttribute("z",
								String.valueOf(location.getZ()));
						root.addContent(squareElement);
					}
				}
			}
		}

		Document doc = new Document(root);
		XMLOutputter outputter = new XMLOutputter();

		try {
			FileWriter file = new FileWriter(new File(DATA_FOLDER + "maps"
					+ File.separator + "newbie" + File.separator
					+ File.separator + map.getName() + ".xml"));
			outputter.output(doc, file);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Tools
	public static Element loadXML(String path) {
		File xmlFile = new File(path);

		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = (Document) builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			return rootNode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
